/*
  RE stuff, in particular for lexing

*/
package main

import "fmt"
import "eh/re"

func example( s string ) {
  fmt.Printf( "example: '%s'\n", s )
  r := re.Parse( s )
  fmt.Printf( ">> RE = %v\n", r )
  // re.MakeSMFor( r ).Display()
  fmt.Println()
}

func fudge( a []string ) []string { return a }

func matchingExample( subject string, reList ...string ) {
  compiled := make([]re.RE, len(reList))
  for i, r := range reList { compiled[i] = re.Parse( r ) }
  sm := re.MakeLexerFor( compiled )
  // sm.Display()
  index, token := sm.Match( subject )
  fmt.Printf( ">> %v matched %s: %v '%s'\n", []string(reList), subject, index, token )
}

func streamExample( subject string, reList ... string ) {
  compiled := make([]re.RE, len(reList))
  for i, r := range reList { compiled[i] = re.Parse( r ) }
  sm := re.MakeLexerFor( compiled )
  for subject != "" {
    index, token := sm.Match( subject )
    if index < 0 {
      fmt.Printf( "BAD THINGS: %s\n", subject )
      subject = subject[1:]
    } else {
      fmt.Printf( "token %s class %d\n", token, index )
      subject = subject[len(token):]
    }
  }
}

func main() {
  fmt.Printf( "trees and the shade.\n" )
  /*
  example( "abc" )
  example( "abc|def|ghi" )
  example( "a*b|c+d|e?f" )
  example( "a(b)c" )
  example( "a(bc)d" )
  example( "a(bc|de)f" )
  */
  streamExample( "tom + harry/19", " ", "[0-9]+", "[+/]", "[a-z]+" )
//
  const WORD = "[a-zA-Z]+"
  const NUM = "[0-9]+"
  const ISOLATE = "[(){}]|\\[|\\]"
  const STRING = `"[^"]*"`
  const OP = "[+*/=<>]+"
  const WS = " +"
  fmt.Printf( "YES IT IS I\n" )
  const COMMENT_PAR =  `#\(` + `([^#]|#+[^#)])*` + `#*` + `#\)`
  const COMMENT_BRACE = `#{` + `([^#]|#+[^#}])*` + `#*` + `#}`
  const COMMENT_BOX =  `#\[` + `([^#]|#+[^#\]])*` + `#*` + `#\]`
  const COMMENT_EOL = `# ` + `[^\n]*` + `\n`
  const COMMENT_PRAG = `#[!#]` + `[^\n]*` + `\n`
  const COMMENT = `(` + COMMENT_PAR + `|` + COMMENT_BRACE + `)` //  + `|` + COMMENT_BRACE + `)` // + `|` + COMMENT_EOL + `)` // + `|` + COMMENT_PRAG + `)`
  matchingExample( `#(#)`, COMMENT )
  matchingExample( `#(##)`, COMMENT )
  matchingExample( `#( #)`, COMMENT )
  matchingExample( `#(  #)`, COMMENT )
  matchingExample( `#( # #)`, COMMENT )
  matchingExample( `#( #### #)`, COMMENT )
  matchingExample( `#( #### ##)`, COMMENT )
  matchingExample( `#(# # #)`, COMMENT )
  matchingExample( `#(## ## ### ## ##)`, COMMENT )
  matchingExample( `#{#}`, COMMENT_BRACE )
  matchingExample( `#{ #}`, COMMENT_BRACE )
  matchingExample( `#{ ### ### ##}`, COMMENT_BRACE )
  // re.MakeLexerFor( []re.RE{re.Parse( STRING )} ).Display()
//  
  // streamExample( `((spoo *= 17){Ambush} "hello"`, WORD, NUM, ISOLATE, STRING, OP, WS ) 
  streamExample( `#(#) #{#} #{24 hours from Tulsa#}`, COMMENT, WS ) 
//  streamExample( `((spoo *= 17){Ambush} "hello"`, WORD, NUM, ISOLATE, STRING, OP, WS ) 
//  streamExample( `((spoo *= 17){Ambush} "hello"`, WORD, NUM, ISOLATE, STRING, OP, WS ) 
//  streamExample( `spoo "hello"`, WORD, STRING, WS ) 
}





